{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "omrGLNyylOxD"
      },
      "outputs": [],
      "source": [
        "import torch"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(torch.cuda.is_available())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bSAwTCvT0bmS",
        "outputId": "cfa2fd4e-32c7-4c20-fdf6-7b41e2a9544c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "False\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# CUDA is an API developed by NVIDIA to interface GPU"
      ],
      "metadata": {
        "id": "R2AXFFJd0bas"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x = torch.randn(10)\n",
        "print(x.device)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f9whLITJ0bKS",
        "outputId": "84a6c8e8-e592-466b-eda5-55a440781b10"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "cpu\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "QRU4qnYolOxE"
      },
      "outputs": [],
      "source": [
        "# how to perform random sampling of the tensors"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "aHvKASaflOxF",
        "outputId": "195b235a-cd2e-4a20-d8a8-d946a39a89d5"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<torch._C.Generator at 0x7f413157ba50>"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ],
      "source": [
        "torch.manual_seed(1234)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "im5Spt1UlOxH",
        "outputId": "503f7232-e9be-4956-eae7-682c27076b13"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.1117, -0.4966,  0.1631, -0.8817],\n",
              "        [ 0.0539,  0.6684, -0.0597, -0.4675],\n",
              "        [-0.2153,  0.8840, -0.7584, -0.3689],\n",
              "        [-0.3424, -1.4020,  0.3206, -1.0219]])"
            ]
          },
          "metadata": {},
          "execution_count": 4
        }
      ],
      "source": [
        "torch.manual_seed(1234)\n",
        "torch.randn(4,4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ekFUX0oslOxI"
      },
      "outputs": [],
      "source": [
        "#generate random numbers from a statistical distribution"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kSo2SOnjlOxI",
        "outputId": "dd7b324a-4004-453c-cdf0-6266d2fbff01"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.2837, 0.6567, 0.2388, 0.7313],\n",
              "        [0.6012, 0.3043, 0.2548, 0.6294],\n",
              "        [0.9665, 0.7399, 0.4517, 0.4757],\n",
              "        [0.7842, 0.1525, 0.6662, 0.3343]])"
            ]
          },
          "metadata": {},
          "execution_count": 6
        }
      ],
      "source": [
        "torch.Tensor(4, 4).uniform_(0, 1) #random number from uniform distribution"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "cJ1in1selOxJ"
      },
      "outputs": [],
      "source": [
        "#now apply the distribution assuming the input values from the \n",
        "#tensor are probabilities"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "r2lilTLrlOxK",
        "outputId": "df75cf14-b5aa-49e9-ee46-51672026f94d"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0., 0., 0., 0.],\n",
              "        [1., 0., 1., 0.],\n",
              "        [1., 0., 1., 1.],\n",
              "        [0., 0., 0., 0.]])"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ],
      "source": [
        "torch.bernoulli(torch.Tensor(4, 4).uniform_(0, 1))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "NixFXUm8lOxK"
      },
      "outputs": [],
      "source": [
        "#how to perform sampling from a multinomial distribution"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5QiMoFXzlOxL",
        "outputId": "4e8cba6f-cbdc-480f-f077-6127363f6f13"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([10., 10., 13., 10., 34., 45., 65., 67., 87., 89., 87., 34.])"
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ],
      "source": [
        "torch.Tensor([10, 10, 13, 10,34,45,65,67,87,89,87,34])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ko_bwKdrlOxM",
        "outputId": "f5348833-d6a3-414d-e17e-f6826c32f8e2"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([4, 5, 7])"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ],
      "source": [
        "torch.multinomial(torch.tensor([10., 10., 13., 10., \n",
        "                                34., 45., 65., 67., \n",
        "                                87., 89., 87., 34.]), \n",
        "                  3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4Z-2q3S7lOxM",
        "outputId": "903257f5-ccdb-457d-f592-6e6eb00bae05"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([10,  5,  9, 10,  5])"
            ]
          },
          "metadata": {},
          "execution_count": 12
        }
      ],
      "source": [
        "torch.multinomial(torch.tensor([10., 10., 13., 10., \n",
        "                                34., 45., 65., 67., \n",
        "                                87., 89., 87., 34.]), \n",
        "                  5, replacement=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "LKo_-nV3lOxN"
      },
      "outputs": [],
      "source": [
        "#generate random numbers from the normal distribution"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PkmfwYNXlOxN",
        "outputId": "b5ab4453-1952-4eda-ece5-9471e9b6fde6"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.5236, 2.2441, 2.7375, 3.9521, 5.4380, 5.5158, 8.2489, 8.1645, 9.0575,\n",
              "        9.8627])"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ],
      "source": [
        "torch.normal(mean=torch.arange(1., 11.), \n",
        "             std=torch.arange(1, 0, -0.1))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "oXu1fQ8VlOxN",
        "outputId": "5a0aef29-c9cf-4bde-f78f-97719fe80d9a"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([ 1.1144,  0.0361,  1.2766, -1.3999, -0.1648])"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ],
      "source": [
        "torch.normal(mean=0.5, \n",
        "             std=torch.arange(1., 6.))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UxK2xVShlOxO",
        "outputId": "7d21742b-65aa-40cd-a06c-a416cdc4e524"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([-0.0844])"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ],
      "source": [
        "torch.normal(mean=0.5, \n",
        "             std=torch.arange(0.2,0.6))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mioQhg_-lOxO",
        "outputId": "b0bd56ea-8d52-413e-c9f0-13bdc733d6d0"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(45.9167)"
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ],
      "source": [
        "#computing the descriptive statistics: mean\n",
        "torch.mean(torch.tensor([10., 10., 13., 10., 34., \n",
        "                         45., 65., 67., 87., 89., 87., 34.]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3gi7FsB1lOxP",
        "outputId": "1e1d3e91-4418-42d5-f6f2-ee52ed2d2ac3"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.6406,  0.9295,  1.2907,  0.2612,  0.9711],\n",
              "        [ 0.3551,  0.8562, -0.3635, -0.1552, -1.2282],\n",
              "        [ 1.2445,  1.1750, -0.2217, -2.0901, -1.2658],\n",
              "        [-1.8761, -0.6066,  0.7470,  0.4811,  0.6234]])"
            ]
          },
          "metadata": {},
          "execution_count": 18
        }
      ],
      "source": [
        "# mean across rows and across columns\n",
        "d = torch.randn(4, 5)\n",
        "d"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6IvPjHWUlOxP",
        "outputId": "9ca2974e-781b-48f1-b231-93301a41910f"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([-0.4793,  0.5885,  0.3631, -0.3757, -0.2249])"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ],
      "source": [
        "torch.mean(d,dim=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CKzLUDMKlOxP",
        "outputId": "e1bde541-6e72-408a-c59d-cd52522c2272"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([ 0.3624, -0.1071, -0.2316, -0.1262])"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ],
      "source": [
        "torch.mean(d,dim=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yPdAnDCHlOxQ",
        "outputId": "bf06580d-09c2-4556-f350-95f05a8353db"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.median(\n",
              "values=tensor([-1.6406,  0.8562, -0.2217, -0.1552, -1.2282]),\n",
              "indices=tensor([0, 1, 2, 1, 1]))"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ],
      "source": [
        "#compute median\n",
        "torch.median(d,dim=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MIL1nnAvlOxQ",
        "outputId": "67ad766e-ec6e-4016-8232-8c3a8b472f48"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.median(\n",
              "values=tensor([ 0.9295, -0.1552, -0.2217,  0.4811]),\n",
              "indices=tensor([1, 3, 2, 3]))"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ],
      "source": [
        "torch.median(d,dim=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5V2sseCblOxQ",
        "outputId": "a2b18314-df83-427a-ebc5-06fc9eceaca5"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.mode(\n",
              "values=tensor([-1.6406, -1.2282, -2.0901, -1.8761]),\n",
              "indices=tensor([0, 4, 3, 0]))"
            ]
          },
          "metadata": {},
          "execution_count": 23
        }
      ],
      "source": [
        "# compute the mode\n",
        "torch.mode(d)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "eAwqQrC5lOxR",
        "outputId": "2a3c9ae8-e20f-4842-a1d1-3deac12f734a"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.mode(\n",
              "values=tensor([-1.8761, -0.6066, -0.3635, -2.0901, -1.2658]),\n",
              "indices=tensor([3, 3, 1, 2, 2]))"
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ],
      "source": [
        "torch.mode(d,dim=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "t9T8cEVNlOxR",
        "outputId": "d7192b35-b0e3-44b4-a08e-501113d29e4e"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.mode(\n",
              "values=tensor([-1.6406, -1.2282, -2.0901, -1.8761]),\n",
              "indices=tensor([0, 4, 3, 0]))"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ],
      "source": [
        "torch.mode(d,dim=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_Qi-Q2pLlOxR",
        "outputId": "a1fd67e9-35a7-45eb-f172-99ace52a658a"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(1.0944)"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ],
      "source": [
        "#compute the standard deviation\n",
        "torch.std(d)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1MXZa3yMlOxS",
        "outputId": "7aebe106-b5ba-4c0d-e44d-ebcfb5c178ed"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.5240, 0.8083, 0.7911, 1.1730, 1.1889])"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ],
      "source": [
        "torch.std(d,dim=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "i9dZjicllOxS",
        "outputId": "e63ba7ce-9751-4e2c-a3ea-b893af51e13f"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.1807, 0.7852, 1.4732, 1.1165])"
            ]
          },
          "metadata": {},
          "execution_count": 28
        }
      ],
      "source": [
        "torch.std(d,dim=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "q4YFfwpPlOxS",
        "outputId": "2cd24f43-b502-4487-8801-f39a8a37a5b0"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(1.1978)"
            ]
          },
          "metadata": {},
          "execution_count": 29
        }
      ],
      "source": [
        "#compute variance\n",
        "torch.var(d)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gZravnGDlOxS",
        "outputId": "70d0eed9-f26a-4cbe-a820-4f102c90adbe"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([2.3224, 0.6534, 0.6259, 1.3758, 1.4134])"
            ]
          },
          "metadata": {},
          "execution_count": 30
        }
      ],
      "source": [
        "torch.var(d,dim=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Wq_RCgmVlOxS",
        "outputId": "dacf2b5a-2c5a-430e-8123-a70b11111dec"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.3940, 0.6166, 2.1703, 1.2466])"
            ]
          },
          "metadata": {},
          "execution_count": 31
        }
      ],
      "source": [
        "torch.var(d,dim=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yfeNnEmMlOxT",
        "outputId": "885dcab6-140b-4d1e-d8cc-a31da2bfe19e"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(-2.0901)"
            ]
          },
          "metadata": {},
          "execution_count": 32
        }
      ],
      "source": [
        "# compute min and max\n",
        "torch.min(d)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Ll8qp443lOxT",
        "outputId": "f3977565-997a-440d-d165-5db65c57e2b4"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.min(\n",
              "values=tensor([-1.8761, -0.6066, -0.3635, -2.0901, -1.2658]),\n",
              "indices=tensor([3, 3, 1, 2, 2]))"
            ]
          },
          "metadata": {},
          "execution_count": 33
        }
      ],
      "source": [
        "torch.min(d,dim=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rqXNNp1dlOxT",
        "outputId": "ac287893-2707-43c9-88c0-69c31cd299c1"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.min(\n",
              "values=tensor([-1.6406, -1.2282, -2.0901, -1.8761]),\n",
              "indices=tensor([0, 4, 3, 0]))"
            ]
          },
          "metadata": {},
          "execution_count": 34
        }
      ],
      "source": [
        "torch.min(d,dim=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zhEQejqwlOxT",
        "outputId": "95e20b83-4743-44bb-87ad-801813c693f5"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(1.2907)"
            ]
          },
          "metadata": {},
          "execution_count": 35
        }
      ],
      "source": [
        "torch.max(d)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JIqkH8e-lOxT",
        "outputId": "d0aedddc-b561-4c20-a39f-7edf8d6cbd54"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.max(\n",
              "values=tensor([1.2445, 1.1750, 1.2907, 0.4811, 0.9711]),\n",
              "indices=tensor([2, 2, 0, 3, 0]))"
            ]
          },
          "metadata": {},
          "execution_count": 36
        }
      ],
      "source": [
        "torch.max(d,dim=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OzjXObVmlOxU",
        "outputId": "944ac37f-e53d-4b59-c962-8e0f6f4bfad6"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.max(\n",
              "values=tensor([1.2907, 0.8562, 1.2445, 0.7470]),\n",
              "indices=tensor([2, 1, 0, 2]))"
            ]
          },
          "metadata": {},
          "execution_count": 37
        }
      ],
      "source": [
        "torch.max(d,dim=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1FkKZf85lOxU",
        "outputId": "6abb196d-3fc4-4f6b-e1c0-0b8035b05e35"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.sort(\n",
              "values=tensor([[-1.6406,  0.2612,  0.9295,  0.9711,  1.2907],\n",
              "        [-1.2282, -0.3635, -0.1552,  0.3551,  0.8562],\n",
              "        [-2.0901, -1.2658, -0.2217,  1.1750,  1.2445],\n",
              "        [-1.8761, -0.6066,  0.4811,  0.6234,  0.7470]]),\n",
              "indices=tensor([[0, 3, 1, 4, 2],\n",
              "        [4, 2, 3, 0, 1],\n",
              "        [3, 4, 2, 1, 0],\n",
              "        [0, 1, 3, 4, 2]]))"
            ]
          },
          "metadata": {},
          "execution_count": 38
        }
      ],
      "source": [
        "# sorting a tensor\n",
        "torch.sort(d)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "j1TzTTp4lOxU",
        "outputId": "b254cbe1-6eae-4dbd-e70d-3fad3407c5ee"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.sort(\n",
              "values=tensor([[-1.8761, -0.6066, -0.3635, -2.0901, -1.2658],\n",
              "        [-1.6406,  0.8562, -0.2217, -0.1552, -1.2282],\n",
              "        [ 0.3551,  0.9295,  0.7470,  0.2612,  0.6234],\n",
              "        [ 1.2445,  1.1750,  1.2907,  0.4811,  0.9711]]),\n",
              "indices=tensor([[3, 3, 1, 2, 2],\n",
              "        [0, 1, 2, 1, 1],\n",
              "        [1, 0, 3, 0, 3],\n",
              "        [2, 2, 0, 3, 0]]))"
            ]
          },
          "metadata": {},
          "execution_count": 39
        }
      ],
      "source": [
        "torch.sort(d,dim=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pB-J5XrnlOxU",
        "outputId": "32be57c6-d0d0-4920-895b-4018933dd2c3"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.sort(\n",
              "values=tensor([[ 1.2445,  1.1750,  1.2907,  0.4811,  0.9711],\n",
              "        [ 0.3551,  0.9295,  0.7470,  0.2612,  0.6234],\n",
              "        [-1.6406,  0.8562, -0.2217, -0.1552, -1.2282],\n",
              "        [-1.8761, -0.6066, -0.3635, -2.0901, -1.2658]]),\n",
              "indices=tensor([[2, 2, 0, 3, 0],\n",
              "        [1, 0, 3, 0, 3],\n",
              "        [0, 1, 2, 1, 1],\n",
              "        [3, 3, 1, 2, 2]]))"
            ]
          },
          "metadata": {},
          "execution_count": 40
        }
      ],
      "source": [
        "torch.sort(d,dim=0,descending=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4FiVXGT3lOxU",
        "outputId": "96857d5e-d1f8-42a5-8a60-711600a459da"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.return_types.sort(\n",
              "values=tensor([[ 1.2907,  0.9711,  0.9295,  0.2612, -1.6406],\n",
              "        [ 0.8562,  0.3551, -0.1552, -0.3635, -1.2282],\n",
              "        [ 1.2445,  1.1750, -0.2217, -1.2658, -2.0901],\n",
              "        [ 0.7470,  0.6234,  0.4811, -0.6066, -1.8761]]),\n",
              "indices=tensor([[2, 4, 1, 3, 0],\n",
              "        [1, 0, 3, 2, 4],\n",
              "        [0, 1, 2, 4, 3],\n",
              "        [2, 4, 3, 1, 0]]))"
            ]
          },
          "metadata": {},
          "execution_count": 41
        }
      ],
      "source": [
        "torch.sort(d,dim=1,descending=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ylndSPY7lOxU"
      },
      "outputs": [],
      "source": [
        "from torch.autograd import Variable"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0i4C-GmrlOxV",
        "outputId": "18b240e6-455d-4541-ddef-d09b40beaab0"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1.],\n",
              "        [1., 1.]], requires_grad=True)"
            ]
          },
          "metadata": {},
          "execution_count": 43
        }
      ],
      "source": [
        "Variable(torch.ones(2,2),requires_grad=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "odD1Jb9VlOxV"
      },
      "outputs": [],
      "source": [
        "a, b = 12,23\n",
        "x1 = Variable(torch.randn(a,b),\n",
        "            requires_grad=True)\n",
        "x2 = Variable(torch.randn(a,b),\n",
        "            requires_grad=True)\n",
        "x3 =Variable(torch.randn(a,b),\n",
        "            requires_grad=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_PfiDQPalOxV",
        "outputId": "a262f932-411a-403d-fae6-3b9e7397f2e5"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "tensor(3278.1235, grad_fn=<SumBackward0>)\n"
          ]
        }
      ],
      "source": [
        "c = x1 * x2\n",
        "d = a + x3\n",
        "e = torch.sum(d)\n",
        "\n",
        "e.backward()\n",
        "\n",
        "print(e)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MY85sfEWlOxV",
        "outputId": "3a021eb4-dd07-4420-eceb-79be4b49c71c"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-4.9545e-02,  6.2245e-01,  1.6573e-01,  3.1583e-01,  2.4915e-01,\n",
              "         -4.9784e-01,  2.9079e+00,  1.6201e+00, -6.4459e-01, -1.9885e-02,\n",
              "          1.6222e+00,  1.4239e+00,  9.0691e-01,  7.6310e-02,  1.1225e+00,\n",
              "         -1.2433e+00, -6.7258e-01,  8.8433e-01, -6.6589e-01, -7.3347e-01,\n",
              "         -2.7599e-01,  5.5485e-01, -1.9303e+00],\n",
              "        [-7.6389e-01, -9.9300e-01,  1.4080e+00,  2.5969e-01,  8.0760e-01,\n",
              "         -1.2618e+00, -7.7109e-01, -1.8497e+00,  2.2400e-01,  2.2088e-01,\n",
              "         -8.2452e-01, -2.5581e-02, -1.9850e+00, -3.7880e-01, -1.5030e+00,\n",
              "         -2.9808e+00,  5.1149e-01, -9.3890e-01, -1.9421e+00,  9.8052e-01,\n",
              "         -7.4463e-02, -7.1181e-01,  1.6136e+00],\n",
              "        [-1.8821e+00, -1.8542e+00, -2.5013e-01,  1.6023e-01,  2.0778e-01,\n",
              "          1.1879e+00, -7.3204e-01,  1.3668e+00,  9.4616e-01,  6.1018e-01,\n",
              "         -1.1084e-01, -2.5730e-01,  6.3652e-01, -4.8517e-01, -7.1437e-01,\n",
              "         -6.0011e-02,  2.5377e-01,  8.2522e-02,  1.0270e+00,  7.0402e-02,\n",
              "          4.2977e-01,  8.2052e-01, -1.4562e+00],\n",
              "        [ 3.0134e-01, -1.9370e+00, -2.9235e-03, -1.0772e+00,  2.5160e-01,\n",
              "          6.6866e-02,  1.4607e+00,  7.7117e-01,  1.2076e-03,  4.2106e-01,\n",
              "          1.0153e+00, -4.9146e-01, -1.1049e+00,  1.9696e-01, -1.4355e-01,\n",
              "          1.4660e+00, -1.0422e-01, -1.3760e-01,  3.7740e-01,  8.5221e-01,\n",
              "         -4.0656e-01, -1.2096e+00,  3.6486e-02],\n",
              "        [ 8.2569e-01, -6.4759e-01, -1.8021e+00,  2.4911e+00, -2.1786e-01,\n",
              "          7.6996e-01, -9.1563e-01,  7.1281e-01,  1.1037e+00, -7.1190e-01,\n",
              "         -2.2254e-01,  3.5406e-01,  5.5607e-01, -6.8683e-01, -9.9216e-01,\n",
              "         -2.4422e-01, -3.4747e-01, -4.0146e-01,  8.2387e-01, -1.0022e+00,\n",
              "         -1.0535e+00, -9.4035e-01, -2.9630e-01],\n",
              "        [-2.3561e+00,  1.1116e-02, -5.8723e-01, -6.1191e-01,  7.4930e-01,\n",
              "         -4.5617e-01, -1.7884e+00, -9.5469e-01,  3.8564e-01,  1.2921e+00,\n",
              "          8.0012e-01,  8.6142e-02, -1.8761e+00,  1.0685e+00, -1.8311e-01,\n",
              "         -1.0058e+00,  3.7973e-01, -2.9853e-01, -1.3616e+00, -3.3329e-01,\n",
              "         -1.2527e+00, -3.2175e-01, -9.4914e-01],\n",
              "        [ 3.4618e-01, -1.2652e+00, -2.1208e+00,  1.5305e+00, -1.0777e+00,\n",
              "         -3.1854e-01, -8.6873e-01, -3.5102e-01, -1.5684e+00,  1.3443e+00,\n",
              "          3.7181e-02, -3.5979e-01,  1.0322e-01, -1.6268e+00, -1.1469e-02,\n",
              "         -1.0545e+00, -2.1433e+00, -5.4195e-01, -4.2554e-01,  1.8125e+00,\n",
              "         -1.3636e+00,  3.0668e-01, -1.8680e+00],\n",
              "        [ 8.1188e-01,  9.2804e-01,  2.5353e+00,  7.4317e-01, -9.2664e-01,\n",
              "          1.0827e+00, -1.0121e+00, -2.4838e-01,  3.5444e-01, -1.0163e-01,\n",
              "         -3.6335e-01,  1.1053e+00, -9.1091e-01, -6.5603e-01, -7.0487e-01,\n",
              "          9.6358e-01,  8.3500e-01, -1.2857e+00, -2.1408e-01,  1.0373e-01,\n",
              "         -4.0387e-01, -4.9104e-01, -9.8583e-01],\n",
              "        [ 1.2580e+00, -1.8494e+00, -9.1556e-01,  1.0041e+00,  4.0680e-01,\n",
              "         -6.7118e-01,  4.5932e-01, -6.7476e-01, -3.6495e-01,  1.2697e+00,\n",
              "         -6.7312e-01, -5.7493e-01,  9.2411e-01,  7.2763e-01,  6.2405e-01,\n",
              "          9.3639e-01,  1.2043e+00,  1.9976e-01, -8.2096e-01, -8.3537e-01,\n",
              "         -3.3319e-01, -1.3532e+00, -4.1707e-02],\n",
              "        [ 7.4626e-01, -1.3165e-01, -6.2212e-01,  1.3070e+00,  1.4547e+00,\n",
              "         -2.1038e-01, -3.4147e-01, -3.6501e-01,  4.9274e-01,  3.4149e-01,\n",
              "          1.3207e+00, -1.0124e+00, -6.5716e-01, -5.7463e-01, -2.0788e+00,\n",
              "         -4.5005e-02,  1.3279e+00,  1.3064e+00, -2.1880e+00, -1.0477e+00,\n",
              "          3.1200e-01, -8.2720e-01, -8.5936e-01],\n",
              "        [-3.9723e-01,  4.2088e-01,  5.3277e-01, -7.2912e-01, -1.1725e+00,\n",
              "          9.1836e-02, -5.6562e-01, -1.1900e+00, -9.4340e-01, -8.1699e-01,\n",
              "         -6.7582e-01, -8.0012e-02,  3.1367e-01, -7.1102e-01,  6.3412e-02,\n",
              "         -3.0735e+00, -8.9330e-01, -2.5040e-01, -3.1167e-02,  1.4725e+00,\n",
              "          1.2173e+00,  6.0895e-01, -1.1243e+00],\n",
              "        [ 7.6267e-01,  8.0859e-01, -9.1959e-01,  1.6713e+00,  7.7652e-01,\n",
              "         -6.4198e-01,  1.2667e+00, -2.4394e-01, -3.8565e-01, -9.5948e-02,\n",
              "          9.4557e-01, -1.2708e+00,  2.9587e-01,  1.5027e-01,  1.1833e+00,\n",
              "          5.7281e-01, -2.5252e-01, -7.3179e-02, -1.4961e-02,  1.4511e-01,\n",
              "          2.0197e-01,  2.7220e-01,  2.9865e-01]])"
            ]
          },
          "metadata": {},
          "execution_count": 46
        }
      ],
      "source": [
        "x1.data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Vnaj-a9clOxV",
        "outputId": "69813e5b-13f6-4456-e847-3d206dee7b40"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-7.5597e-01, -1.1689e+00, -9.3890e-01,  8.8566e-01,  1.3764e+00,\n",
              "         -7.8276e-01,  2.2200e-01,  7.3758e-02, -6.9147e-01, -5.1308e-01,\n",
              "          1.1427e+00, -1.0126e+00,  1.1602e-01, -1.0350e+00,  1.0803e+00,\n",
              "         -7.9977e-01, -9.1219e-02,  5.0242e-01, -4.5173e-01, -4.8067e-01,\n",
              "          5.9066e-01,  1.6343e-01, -3.1368e-02],\n",
              "        [ 4.4646e-01, -2.1036e+00, -1.8971e+00,  1.4661e+00,  6.1199e-01,\n",
              "          7.9935e-01,  9.3007e-01,  1.2819e+00, -6.2896e-01,  3.0550e-01,\n",
              "          5.8429e-01, -2.1502e+00,  4.1505e-01,  3.1579e-03, -3.8580e-01,\n",
              "          5.7681e-01, -1.1972e+00, -2.4006e-01, -1.3253e+00,  1.1415e+00,\n",
              "         -4.4164e-01,  3.4923e-01, -1.6730e+00],\n",
              "        [ 1.7173e+00, -1.0184e+00,  1.1152e+00, -5.0580e-01,  1.7331e-01,\n",
              "         -6.8128e-01,  2.5642e-02,  1.2838e+00,  1.3107e+00,  1.4736e+00,\n",
              "          1.3023e+00, -1.4379e+00, -1.7115e+00, -8.0051e-01,  1.4921e+00,\n",
              "         -1.0768e-01,  1.8001e+00,  9.7892e-01, -5.9166e-01,  8.5103e-02,\n",
              "          7.5238e-02,  8.3984e-01,  1.3866e+00],\n",
              "        [-9.5211e-01, -4.9072e-01, -1.4583e+00,  1.3491e+00,  1.5738e+00,\n",
              "          1.2403e+00,  8.7063e-01, -7.4153e-01,  1.7150e+00,  5.9718e-01,\n",
              "          1.1104e-01,  5.8702e-01,  1.7935e+00, -4.3749e-01, -4.4637e-01,\n",
              "         -7.3716e-01,  6.1647e-02, -2.5027e-01, -1.4518e-01, -1.9606e-01,\n",
              "         -5.0812e-01, -8.0779e-01,  1.6439e+00],\n",
              "        [-1.2366e+00,  5.9530e-02, -1.3995e-01,  6.6461e-02, -1.6116e+00,\n",
              "          1.0756e+00, -5.2225e-02,  1.0433e+00, -1.7803e-01,  5.4113e-01,\n",
              "          1.5247e+00, -2.2933e-02, -1.0489e+00,  5.9934e-01, -1.1722e+00,\n",
              "         -3.5785e-01,  2.7906e+00, -1.8163e-01,  5.1971e-01, -3.1983e-01,\n",
              "          1.2022e+00, -4.8330e-01, -2.8758e-01],\n",
              "        [ 1.0301e+00,  7.2947e-01,  1.0306e-01,  2.7964e-01,  1.2181e+00,\n",
              "          5.6054e-01,  8.6046e-01, -1.0222e+00, -1.1600e+00,  7.2069e-01,\n",
              "          1.4963e-01, -8.3145e-01, -1.9167e+00, -1.8408e+00,  9.6285e-01,\n",
              "          2.2254e-02, -1.4754e+00,  4.3352e-01, -2.3785e-01,  3.4293e-01,\n",
              "         -2.3405e+00,  3.1645e-01,  1.4717e+00],\n",
              "        [-3.9061e-01,  9.7402e-02,  1.0617e+00,  1.2896e+00,  7.8817e-01,\n",
              "         -1.6625e+00, -1.1102e+00,  7.6015e-03, -1.0987e+00, -5.3510e-01,\n",
              "         -2.0040e-03,  1.0430e+00, -2.6981e+00, -5.0815e-01, -7.9794e-01,\n",
              "          9.4735e-01,  5.9186e-01, -4.8238e-01,  8.9618e-02,  5.0087e-01,\n",
              "          1.3167e-01,  1.2114e-01, -1.1848e+00],\n",
              "        [-1.4867e+00, -1.1119e+00, -5.5689e-01, -1.9381e-01,  6.6268e-01,\n",
              "         -3.6233e-01,  9.7089e-01,  1.5987e+00,  4.4115e-01,  1.3639e+00,\n",
              "          3.1804e-01, -1.6259e-01, -4.1701e-02,  2.7475e-01, -6.1249e-01,\n",
              "          2.1635e+00, -6.9394e-01, -6.7843e-01, -1.4126e+00, -6.1121e-01,\n",
              "         -2.7585e-01, -4.9035e-01,  2.5709e-01],\n",
              "        [ 1.1294e+00,  1.4856e+00, -6.7626e-01,  9.3721e-02, -6.6421e-02,\n",
              "         -1.0708e+00, -6.1524e-01, -4.2784e-01,  7.8178e-01, -6.3566e-01,\n",
              "         -3.9345e-01,  8.0382e-01,  5.0099e-01, -6.9284e-01, -1.0052e-01,\n",
              "         -8.0754e-01,  5.8784e-01,  1.2261e+00,  1.1008e+00, -3.8728e-01,\n",
              "          3.2881e-01,  6.4573e-01, -2.1029e-01],\n",
              "        [-9.9325e-01, -8.6854e-01,  7.7369e-02,  1.1841e+00,  5.0544e-01,\n",
              "         -5.6922e-01,  1.1186e+00,  1.0655e+00,  5.1852e-01, -2.0170e+00,\n",
              "         -1.3915e+00,  1.3255e+00,  4.3642e-01, -1.7325e+00,  4.2292e-01,\n",
              "          2.0095e-01, -6.1319e-01,  1.4466e+00, -1.2209e+00,  4.0064e-01,\n",
              "          3.0691e-01,  5.4840e-01,  2.1183e+00],\n",
              "        [-7.8591e-01, -6.0411e-01,  2.4474e-01, -2.3646e-01,  1.1019e+00,\n",
              "         -1.5382e+00,  8.6998e-02, -6.7848e-01,  7.1260e-01,  1.1081e+00,\n",
              "          7.9302e-01,  3.6504e-01, -7.8480e-01, -3.5138e-01,  5.5498e-01,\n",
              "         -2.7474e-01,  4.1603e-01, -1.3930e+00, -8.6290e-01, -2.4129e-01,\n",
              "         -1.2446e+00, -2.1552e+00,  6.1390e-01],\n",
              "        [ 8.6510e-01, -3.1663e-02, -6.4619e-02, -3.0712e-01,  2.0007e-01,\n",
              "          4.0111e-01, -1.2684e+00,  3.3103e-01,  6.2498e-01, -7.7015e-01,\n",
              "          1.0212e+00,  7.1803e-01, -1.0951e-01, -4.4673e-02,  3.4933e-01,\n",
              "         -1.4548e-01,  9.7760e-01,  3.8946e-01, -6.9499e-01, -8.3517e-02,\n",
              "         -5.6924e-02,  1.1568e+00, -1.2049e+00]])"
            ]
          },
          "metadata": {},
          "execution_count": 47
        }
      ],
      "source": [
        "x2.data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Dq1rnJpYlOxW",
        "outputId": "83899120-cc80-4b9a-b145-6b66c4134d1b"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.2499,  0.2458,  0.1029, -0.6494, -0.3258,  0.8149,  0.4049,  0.2481,\n",
              "          0.4841,  0.3293, -1.2471,  0.2117,  1.4315,  0.0502, -0.3668,  0.8378,\n",
              "         -0.7901,  0.0267, -0.3120,  2.4534,  0.7926,  0.2382, -0.5245],\n",
              "        [-0.2131, -2.0323, -0.3952,  0.7286,  1.3579,  1.3583, -0.5818, -0.3204,\n",
              "         -0.7242, -1.4629,  0.6109,  0.2401, -0.5455,  2.9154, -0.1985,  0.7507,\n",
              "         -0.0390, -0.8322,  0.1364,  0.4531, -0.1112,  1.0307, -1.1862],\n",
              "        [-0.9696,  0.1508,  0.9814, -1.9638, -0.2218,  0.1477, -0.1875,  0.9963,\n",
              "          0.3611,  0.0238,  1.7371, -0.7392, -0.1709,  1.2120,  0.9385,  0.3245,\n",
              "         -1.7054,  0.7467, -1.4787, -0.2400,  0.4558, -1.0911,  0.1566],\n",
              "        [-1.0629,  0.7770,  0.4003, -0.7194,  0.3499, -0.3935, -0.3622, -3.4058,\n",
              "         -0.1108, -0.7741,  0.9103, -0.0961, -0.6697, -0.1985, -1.1560, -0.7709,\n",
              "         -0.4655, -0.3847, -0.4364, -0.7429, -0.5210, -0.8078, -0.4448],\n",
              "        [ 1.4265,  0.2566,  0.4157,  1.7485, -0.5296, -0.7132, -0.1676, -0.6071,\n",
              "          0.0347, -0.9721, -1.6722,  1.7052,  0.3938, -0.7728, -0.6024, -0.3242,\n",
              "         -0.6425, -2.3893, -2.6920, -0.2646,  0.4027, -1.0844,  0.1360],\n",
              "        [ 2.1896, -0.2159, -0.4342, -0.2216, -0.7294, -0.1235,  0.6418, -2.1429,\n",
              "         -0.0135,  0.9174, -1.8431,  0.6344,  0.8557, -1.1231, -0.6695,  0.0306,\n",
              "         -2.3209,  1.9433, -1.7311, -1.2552,  0.4064,  1.6822,  1.3038],\n",
              "        [-0.3663, -1.2401, -0.2566, -0.0977,  0.2273, -1.5666, -0.7700, -0.1483,\n",
              "         -0.8830, -1.5841, -0.3711, -1.2338, -0.6142,  0.2801,  0.0178,  0.3047,\n",
              "          0.8214, -2.6705,  0.3366, -0.4533,  0.0461, -0.2598,  0.7919],\n",
              "        [-0.8666, -0.3528, -1.5244, -0.4039, -1.4359,  0.7409,  1.1551, -1.2673,\n",
              "         -0.1306, -0.3743, -1.1143, -1.0104,  0.5946,  0.3441,  1.5870, -0.4498,\n",
              "          0.0428,  0.2114,  0.3053,  0.1590,  0.6634, -0.2352,  0.4823],\n",
              "        [ 1.2331,  0.0465, -0.0451,  0.6444,  1.6787,  0.8955,  0.5074, -0.3619,\n",
              "          1.4870, -1.2376, -0.0362, -0.2950, -2.5790, -1.0069,  1.8991,  0.0619,\n",
              "          0.2949,  0.0973, -0.1098,  1.5281, -0.4748, -0.5837, -0.2411],\n",
              "        [ 0.9073, -0.2173,  0.4118, -1.8131, -0.7694, -0.7774, -0.4924, -0.7196,\n",
              "         -0.1061, -0.2974, -0.6724,  0.8060,  0.1783, -0.8092,  0.2455, -0.1944,\n",
              "         -0.3686, -0.2207,  1.3099, -1.3060, -1.4669, -0.3317, -0.4190],\n",
              "        [-1.4119, -2.5146,  2.0673, -1.0484,  0.4922,  1.7666,  0.0404,  0.4461,\n",
              "         -1.6570, -0.6111, -0.7872,  0.7080, -0.4652,  0.5982,  0.2778,  1.7961,\n",
              "          0.7223,  0.5924, -0.8698,  0.4279,  1.7435, -0.2529, -1.1322],\n",
              "        [-0.5567,  0.1003,  0.1121,  1.2011, -0.7812, -0.6869, -0.1129, -0.2705,\n",
              "         -3.0171,  0.0981, -0.1275,  0.1092, -0.2081, -1.7573,  0.3798, -0.5517,\n",
              "         -0.1958, -0.7510,  1.3651,  0.3416,  1.2432,  0.8636, -0.2051]])"
            ]
          },
          "metadata": {},
          "execution_count": 48
        }
      ],
      "source": [
        "x3.data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WU_q483FlOxW",
        "outputId": "3dd2f92b-ba59-4d00-fea6-2c2c18fd6297"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Gradient of w1 w.r.t to Loss: -455.0\n",
            "Gradient of w2 w.r.t to Loss: -365.0\n",
            "Gradient of w3 w.r.t to Loss: -60.0\n",
            "Gradient of w4 w.r.t to Loss: -265.0\n"
          ]
        }
      ],
      "source": [
        "from torch import FloatTensor\n",
        "from torch.autograd import Variable\n",
        "\n",
        "a = Variable(FloatTensor([5]))\n",
        "\n",
        "weights = [Variable(FloatTensor([i]), requires_grad=True) for i in (12, 53, 91, 73)]\n",
        "\n",
        "w1, w2, w3, w4 = weights\n",
        "\n",
        "b = w1 * a\n",
        "c = w2 * a\n",
        "d = w3 * b + w4 * c\n",
        "Loss = (10 - d)\n",
        "\n",
        "Loss.backward()\n",
        "\n",
        "for index, weight in enumerate(weights, start=1):\n",
        "    gradient, *_ = weight.grad.data\n",
        "    print(f\"Gradient of w{index} w.r.t to Loss: {gradient}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Kv6j2By7lOxW"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "YgkJRNsAlOxW"
      },
      "outputs": [],
      "source": [
        "# Using forward pass\n",
        "def forward(x):\n",
        "    return x * w"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_vKnWJfRlOxW",
        "outputId": "567b4f44-07ba-4ad4-930f-ceebbb2e3389"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "predict (before training) 4 tensor(4.)\n"
          ]
        }
      ],
      "source": [
        "import torch\n",
        "from torch.autograd import Variable\n",
        "\n",
        "x_data = [11.0, 22.0, 33.0]\n",
        "y_data = [21.0, 14.0, 64.0]\n",
        "\n",
        "w = Variable(torch.Tensor([1.0]),  requires_grad=True)  # Any random value\n",
        "\n",
        "# Before training\n",
        "print(\"predict (before training)\",  4, forward(4).data[0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gR-GPl9alOxX"
      },
      "outputs": [],
      "source": [
        "# define the Loss function\n",
        "def loss(x, y):\n",
        "    y_pred = forward(x)\n",
        "    return (y_pred - y) * (y_pred - y)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5C0AjcmXlOxX",
        "outputId": "6dea7b8b-8912-47f3-88e4-9ed5b3cd10ec"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\tgrad:  11.0 21.0 tensor(-220.)\n",
            "\tgrad:  22.0 14.0 tensor(2481.6001)\n",
            "\tgrad:  33.0 64.0 tensor(-51303.6484)\n",
            "progress: 0 tensor(604238.8125)\n",
            "\tgrad:  11.0 21.0 tensor(118461.7578)\n",
            "\tgrad:  22.0 14.0 tensor(-671630.6875)\n",
            "\tgrad:  33.0 64.0 tensor(13114108.)\n",
            "progress: 1 tensor(3.9481e+10)\n",
            "\tgrad:  11.0 21.0 tensor(-30279010.)\n",
            "\tgrad:  22.0 14.0 tensor(1.7199e+08)\n",
            "\tgrad:  33.0 64.0 tensor(-3.3589e+09)\n",
            "progress: 2 tensor(2.5900e+15)\n",
            "\tgrad:  11.0 21.0 tensor(7.7553e+09)\n",
            "\tgrad:  22.0 14.0 tensor(-4.4050e+10)\n",
            "\tgrad:  33.0 64.0 tensor(8.6030e+11)\n",
            "progress: 3 tensor(1.6991e+20)\n",
            "\tgrad:  11.0 21.0 tensor(-1.9863e+12)\n",
            "\tgrad:  22.0 14.0 tensor(1.1282e+13)\n",
            "\tgrad:  33.0 64.0 tensor(-2.2034e+14)\n",
            "progress: 4 tensor(1.1146e+25)\n",
            "\tgrad:  11.0 21.0 tensor(5.0875e+14)\n",
            "\tgrad:  22.0 14.0 tensor(-2.8897e+15)\n",
            "\tgrad:  33.0 64.0 tensor(5.6436e+16)\n",
            "progress: 5 tensor(7.3118e+29)\n",
            "\tgrad:  11.0 21.0 tensor(-1.3030e+17)\n",
            "\tgrad:  22.0 14.0 tensor(7.4013e+17)\n",
            "\tgrad:  33.0 64.0 tensor(-1.4455e+19)\n",
            "progress: 6 tensor(4.7966e+34)\n",
            "\tgrad:  11.0 21.0 tensor(3.3374e+19)\n",
            "\tgrad:  22.0 14.0 tensor(-1.8957e+20)\n",
            "\tgrad:  33.0 64.0 tensor(3.7022e+21)\n",
            "progress: 7 tensor(inf)\n",
            "\tgrad:  11.0 21.0 tensor(-8.5480e+21)\n",
            "\tgrad:  22.0 14.0 tensor(4.8553e+22)\n",
            "\tgrad:  33.0 64.0 tensor(-9.4824e+23)\n",
            "progress: 8 tensor(inf)\n",
            "\tgrad:  11.0 21.0 tensor(2.1894e+24)\n",
            "\tgrad:  22.0 14.0 tensor(-1.2436e+25)\n",
            "\tgrad:  33.0 64.0 tensor(2.4287e+26)\n",
            "progress: 9 tensor(inf)\n"
          ]
        }
      ],
      "source": [
        "# Run the Training loop\n",
        "for epoch in range(10):\n",
        "    for x_val, y_val in zip(x_data, y_data):\n",
        "        l = loss(x_val, y_val)\n",
        "        l.backward()\n",
        "        print(\"\\tgrad: \", x_val, y_val, w.grad.data[0])\n",
        "        w.data = w.data - 0.01 * w.grad.data\n",
        "\n",
        "        # Manually set the gradients to zero after updating weights\n",
        "        w.grad.data.zero_()\n",
        "\n",
        "    print(\"progress:\", epoch, l.data[0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Z6Q0Lq1TlOxX",
        "outputId": "f559d811-e055-4798-daa6-4c44fd8a241e"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "predict (after training) 4 tensor(-9.2687e+24)\n"
          ]
        }
      ],
      "source": [
        "# After training\n",
        "print(\"predict (after training)\",  4, forward(4).data[0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "M2dEQtSzlOxX",
        "outputId": "42ecd0bf-e857-47d6-be31-faf4ae7a07f7"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "tensor([[-0.3071, -3.6691, -2.8417, -1.1818],\n",
            "        [-1.4654, -0.4344, -2.0130, -2.3842],\n",
            "        [ 1.3962,  1.4962, -2.0996,  1.8881],\n",
            "        [-1.9797,  0.2337, -1.0308,  0.1266]])\n"
          ]
        }
      ],
      "source": [
        "z = Variable(torch.Tensor(4, 4).uniform_(-5, 5))\n",
        "print(z)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "E1pLWZk8lOxY",
        "outputId": "bd37d1c8-1c98-4c94-ec86-7fb84c235fcc"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Requires Gradient : False \n",
            "Volatile : False \n",
            "Gradient : None \n",
            "tensor([[-0.3071, -3.6691, -2.8417, -1.1818],\n",
            "        [-1.4654, -0.4344, -2.0130, -2.3842],\n",
            "        [ 1.3962,  1.4962, -2.0996,  1.8881],\n",
            "        [-1.9797,  0.2337, -1.0308,  0.1266]])\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: UserWarning: volatile was removed (Variable.volatile is always False)\n",
            "  \n"
          ]
        }
      ],
      "source": [
        "print('Requires Gradient : %s ' % (z.requires_grad))\n",
        "print('Volatile : %s ' % (z.volatile))\n",
        "print('Gradient : %s ' % (z.grad))\n",
        "print(z.data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JSrWTkLSlOxY",
        "outputId": "e66d5f18-f574-4655-f4e2-e5cb6f4812c2"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "torch.Size([4, 4])\n"
          ]
        }
      ],
      "source": [
        "x = Variable(torch.Tensor(4, 4).uniform_(-4, 5))\n",
        "y = Variable(torch.Tensor(4, 4).uniform_(-3, 2))\n",
        "# matrix multiplication\n",
        "z = torch.mm(x, y)\n",
        "print(z.size())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "THb7ZbbklOxY",
        "outputId": "9cd93d2f-ac65-4e85-abe2-514ebb9455e8"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 4.1714, -1.3202,  4.5641,  0.2945],\n",
              "        [-2.5585,  2.0775,  1.0064,  4.9148],\n",
              "        [ 3.9464, -3.6807,  4.3685,  4.6255],\n",
              "        [ 2.8453,  0.2704,  4.8619,  0.7903]])"
            ]
          },
          "metadata": {},
          "execution_count": 58
        }
      ],
      "source": [
        "x.data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "esesJDtllOxY"
      },
      "outputs": [],
      "source": [
        "#tensor operations"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f66PpVEvlOxZ",
        "outputId": "bab93004-72a5-4200-abbe-9956e513a631"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.9002, 0.9188, 0.1386, 0.3701],\n",
              "        [0.1947, 0.2268, 0.9587, 0.2615],\n",
              "        [0.7256, 0.7673, 0.5667, 0.1863],\n",
              "        [0.4642, 0.4016, 0.9981, 0.8452]])"
            ]
          },
          "metadata": {},
          "execution_count": 60
        }
      ],
      "source": [
        "mat1 = torch.FloatTensor(4,4).uniform_(0,1)\n",
        "mat1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "omnV7gP6lOxZ",
        "outputId": "45ce18f9-ab70-42e7-964b-91acfdc090b5"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.4962, 0.4947, 0.8344, 0.6721],\n",
              "        [0.1182, 0.5997, 0.8990, 0.8252],\n",
              "        [0.1466, 0.1093, 0.8135, 0.9047],\n",
              "        [0.2486, 0.1873, 0.6159, 0.2471]])"
            ]
          },
          "metadata": {},
          "execution_count": 72
        }
      ],
      "source": [
        "mat2 = torch.FloatTensor(4,4).uniform_(0,1)\n",
        "mat2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kvQexmLLlOxZ",
        "outputId": "040e431f-678e-4e61-b0ab-54610f6b0df2"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0.7582, 0.6879, 0.8949, 0.3995])"
            ]
          },
          "metadata": {},
          "execution_count": 73
        }
      ],
      "source": [
        "vec1 = torch.FloatTensor(4).uniform_(0,1)\n",
        "vec1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "eIdac4aelOxZ"
      },
      "outputs": [],
      "source": [
        "# scalar addition"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "YwAXVw-0lOxZ",
        "outputId": "46032e73-88d2-4e04-f856-229bc59c7c7b"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[11.4002, 11.4188, 10.6386, 10.8701],\n",
              "        [10.6947, 10.7268, 11.4587, 10.7615],\n",
              "        [11.2256, 11.2673, 11.0667, 10.6863],\n",
              "        [10.9642, 10.9016, 11.4981, 11.3452]])"
            ]
          },
          "metadata": {},
          "execution_count": 75
        }
      ],
      "source": [
        "mat1 + 10.5"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ksx-522ylOxZ"
      },
      "outputs": [],
      "source": [
        "# scalar subtraction"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lPQfHCJAlOxa",
        "outputId": "e7e30d31-1028-4fd2-e89c-8451b75e21c2"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.2962,  0.2947,  0.6344,  0.4721],\n",
              "        [-0.0818,  0.3997,  0.6990,  0.6252],\n",
              "        [-0.0534, -0.0907,  0.6135,  0.7047],\n",
              "        [ 0.0486, -0.0127,  0.4159,  0.0471]])"
            ]
          },
          "metadata": {},
          "execution_count": 77
        }
      ],
      "source": [
        "mat2 - 0.20"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "uBMORUwalOxa"
      },
      "outputs": [],
      "source": [
        "# vector and matrix addition"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "e28tx8ailOxa",
        "outputId": "3cd9c421-30c5-47a5-cbd1-b4bbf4a9b845"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1.6584, 1.6067, 1.0335, 0.7695],\n",
              "        [0.9530, 0.9147, 1.8537, 0.6610],\n",
              "        [1.4839, 1.4553, 1.4616, 0.5858],\n",
              "        [1.2224, 1.0895, 1.8931, 1.2446]])"
            ]
          },
          "metadata": {},
          "execution_count": 79
        }
      ],
      "source": [
        "mat1 + vec1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LG9e5d2NlOxa",
        "outputId": "98e97de3-0db2-40ed-c7ca-4d272be97fed"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1.2544, 1.1826, 1.7293, 1.0716],\n",
              "        [0.8764, 1.2876, 1.7939, 1.2247],\n",
              "        [0.9049, 0.7972, 1.7084, 1.3042],\n",
              "        [1.0068, 0.8752, 1.5108, 0.6466]])"
            ]
          },
          "metadata": {},
          "execution_count": 80
        }
      ],
      "source": [
        "mat2 + vec1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "WKVWHJpklOxa"
      },
      "outputs": [],
      "source": [
        "# matrix-matrix addition"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "23pMreZclOxa",
        "outputId": "7d1f2523-f173-4120-df1c-aa0c02837449"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1.3963, 1.4135, 0.9730, 1.0422],\n",
              "        [0.3129, 0.8265, 1.8577, 1.0867],\n",
              "        [0.8722, 0.8766, 1.3802, 1.0910],\n",
              "        [0.7127, 0.5888, 1.6141, 1.0923]])"
            ]
          },
          "metadata": {},
          "execution_count": 82
        }
      ],
      "source": [
        "mat1 + mat2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UaFGpnDRlOxb",
        "outputId": "97068dad-8a0a-4e3a-8f2a-ea96b485f413"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.8103, 0.8442, 0.0192, 0.1370],\n",
              "        [0.0379, 0.0514, 0.9192, 0.0684],\n",
              "        [0.5265, 0.5888, 0.3211, 0.0347],\n",
              "        [0.2155, 0.1613, 0.9963, 0.7143]])"
            ]
          },
          "metadata": {},
          "execution_count": 83
        }
      ],
      "source": [
        "mat1 * mat1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "iB9qGUzjlOxb"
      },
      "outputs": [],
      "source": [
        "# about Bernoulli distribution"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Le4cbcF-lOxb"
      },
      "outputs": [],
      "source": [
        "from torch.distributions.bernoulli import Bernoulli"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Z4Z_VOeilOxb"
      },
      "outputs": [],
      "source": [
        "dist = Bernoulli(torch.tensor([0.3,0.6,0.9]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rC-Czw1dlOxb",
        "outputId": "6a03e406-86aa-48fa-a697-e6fbcc4b672a"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0., 1., 0.])"
            ]
          },
          "metadata": {},
          "execution_count": 87
        }
      ],
      "source": [
        "dist.sample() #sample is binary, it takes 1 with p and 0 with 1-p"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "l89yVuwrlOxb"
      },
      "outputs": [],
      "source": [
        "#Creates a Bernoulli distribution parameterized by probs \n",
        "#Samples are binary (0 or 1). They take the value 1 with probability p \n",
        "#and 0 with probability 1 - p."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "JI3ktUbDlOxb"
      },
      "outputs": [],
      "source": [
        "from torch.distributions.beta import Beta"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "b94WrCrWlOxc",
        "outputId": "03f86a9b-e467-46ff-abec-e2b49ae05409"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Beta()"
            ]
          },
          "metadata": {},
          "execution_count": 90
        }
      ],
      "source": [
        "dist = Beta(torch.tensor([0.5]), torch.tensor([0.5]))\n",
        "dist"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-dGCXUAMlOxc",
        "outputId": "077f0007-2aee-4ad8-fb9e-7a724b0f8184"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0.6771])"
            ]
          },
          "metadata": {},
          "execution_count": 91
        }
      ],
      "source": [
        "dist.sample()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5GGiU-wwlOxc"
      },
      "outputs": [],
      "source": [
        "from torch.distributions.binomial import Binomial"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "sf0ndW6tlOxc"
      },
      "outputs": [],
      "source": [
        "dist = Binomial(100, torch.tensor([0 , .2, .8, 1]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8EB23HpXlOxc",
        "outputId": "623ebece-db7b-433e-ee0c-7cb0e0230c85"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([  0.,  21.,  83., 100.])"
            ]
          },
          "metadata": {},
          "execution_count": 94
        }
      ],
      "source": [
        "dist.sample()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "W2jx1FfvlOxc"
      },
      "outputs": [],
      "source": [
        "# 100- count of trials\n",
        "# 0, 0.2, 0.8 and 1 are event probabilities"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "reuCxuaulOxd"
      },
      "outputs": [],
      "source": [
        "from torch.distributions.categorical import Categorical"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fzFDxbdylOxd",
        "outputId": "fa6d937d-023d-4cdd-a5e0-d82b416b94fa"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Categorical(probs: torch.Size([5]))"
            ]
          },
          "metadata": {},
          "execution_count": 97
        }
      ],
      "source": [
        "dist = Categorical(torch.tensor([ 0.20, 0.20, 0.20, 0.20, 0.20 ]))\n",
        "dist"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MJhxIJwElOxd",
        "outputId": "0ad16ed9-d260-4034-a38e-a6a2b0b36ba7"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(2)"
            ]
          },
          "metadata": {},
          "execution_count": 98
        }
      ],
      "source": [
        "dist.sample()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "eSE7XhculOxd"
      },
      "outputs": [],
      "source": [
        "# 0.20, 0.20, 0.20, 0.20,0.20 event probabilities"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "KVOXshFBlOxd"
      },
      "outputs": [],
      "source": [
        "# Laplace distribution parameterized by loc and ‘scale’."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "yMv-qmfJlOxl"
      },
      "outputs": [],
      "source": [
        "from torch.distributions.laplace import Laplace"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IvxaNdI-lOxl",
        "outputId": "e14d7961-5b09-4d06-f6a1-67af1dc69465"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Laplace(loc: tensor([10.]), scale: tensor([0.9900]))"
            ]
          },
          "metadata": {},
          "execution_count": 102
        }
      ],
      "source": [
        "dist = Laplace(torch.tensor([10.0]), torch.tensor([0.990]))\n",
        "dist"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "y7aFsTJglOxl",
        "outputId": "df9a0c7b-91dc-4ef0-a98d-4f17f1109f41"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([9.6554])"
            ]
          },
          "metadata": {},
          "execution_count": 103
        }
      ],
      "source": [
        "dist.sample()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "4iIJEjrMlOxm"
      },
      "outputs": [],
      "source": [
        "#Normal (Gaussian) distribution parameterized by loc and ‘scale’."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "t9nWn9PQlOxm"
      },
      "outputs": [],
      "source": [
        "from torch.distributions.normal import Normal"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wXcIsBNilOxm",
        "outputId": "cbda0cf5-2973-4689-86bf-0e5a34d0e080"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Normal(loc: tensor([100.]), scale: tensor([10.]))"
            ]
          },
          "metadata": {},
          "execution_count": 106
        }
      ],
      "source": [
        "dist = Normal(torch.tensor([100.0]), torch.tensor([10.0]))\n",
        "dist"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SMn_25hslOxm",
        "outputId": "a356ca71-2da2-4d80-9839-73c29b97969f"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([84.3435])"
            ]
          },
          "metadata": {},
          "execution_count": 107
        }
      ],
      "source": [
        "dist.sample()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "QTmYev-XlOxm"
      },
      "outputs": [],
      "source": [
        ""
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.4"
    },
    "colab": {
      "name": "Torch_AI_2_2Ed.ipynb",
      "provenance": [],
      "collapsed_sections": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}